<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
<!-- 统计字符串字符出现的次数 -->
</body>
</html>
<script>
    // 方法一
    function census(str) {
         let res = {};
         for (let i = 0; i < str.length; i++) {
             if (res[str[i]]) {
                 res[str[i]]++;
             } else {
                 res[str[i]] = 1;
             }
         }
         return res;
     }
     console.log(census("abbbccd"));//{a: 1, b: 3, c: 2, d: 1}


    // 方法二
    function census(str) {
        let arr = [...str];
        /* console.log(arr);//['a', 'b', 'b', 'b', 'c', 'c', 'd'] */
        let res = arr.reduce(function (sum,item) {
            if (sum[item]) {
                sum[item]++;
            } else {
                sum[item] = 1;
            }
            return sum;
        }, {})
        console.log(res);
    }
    census("abbcdd")  //{a: 1, b: 2, c: 1, d: 2}

    // 空对象中不存在这键,则赋值为1.存在自增
</script>